package com.wanchuang.hepos.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.wanchuang.architecture.utils.Utils;
import com.wanchuang.hepos.BuildConfig;
import com.wanchuang.hepos.R;
import com.wanchuang.hepos.app.App;
import com.wanchuang.hepos.help.DialogUtils;
import com.wanchuang.hepos.help.PreferenceSettings;
import com.wanchuang.hepos.help.UserHelper;
import com.wanchuang.hepos.bridge.data.repository.Apis;
import com.wanchuang.hepos.net.NetworkManager;
import com.wanchuang.hepos.net.interfaces.OnRequestCallBack;
import com.wanchuang.hepos.proto.Index;
import com.wanchuang.hepos.ui.page.glide.GlideFragment;
import com.google.protobuf.InvalidProtocolBufferException;

public class StartActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        ((App) (Utils.getApp())).setServiceHost(BuildConfig.SERVERHEAD);
        NetworkManager.INSTANCE.post(Apis.get_site_domain, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                ((App) (Utils.getApp())).setServiceHost(Index.get_site_domain.parseFrom(response).getHost());
                new Handler().postDelayed(() -> {
                    if (PreferenceSettings.getWelcome()) {
                        FragmentManager manager = getSupportFragmentManager();
                        FragmentTransaction transaction = manager.beginTransaction();
                        transaction.setCustomAnimations(R.anim.h_fragment_enter, R.anim.h_fragment_exit);
                        GlideFragment glideFragment = new GlideFragment();
                        transaction.add(R.id.fl_fragment, glideFragment, "glide");
                        transaction.commitAllowingStateLoss();
                    } else {
                        Log.e("getIsRenZhen",UserHelper.getIsRenZhen());
                        if (UserHelper.isLogin() && UserHelper.getIsRenZhen().equals("2")) {
                            startActivity(new Intent(StartActivity.this, MainActivity.class));
                        } else {
                            startActivity(new Intent(StartActivity.this, LoginActivity.class));
                        }
                        finish();
                    }
//
                }, 1000);
            }

            @Override
            public void onError(int code, String errorMessage) {
                DialogUtils.showHitDialog(StartActivity.this, "提示", errorMessage);
            }
        });

    }
}
